﻿package connection
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class Packet
	{
		public var headerFlag:int;
		public var headerDataLength:int;
		public var commandCode:String;
		public var commandType:int;
		public var commandSenderID:int;
		public var contentData:String;

		public function Packet()
		{

		}
		public function printPacket():void
		{
			trace("headerFlag : "+ headerFlag);
			trace("headerDataLength :" + headerDataLength);
			trace("commandCode : "+ commandCode);
			trace("commandType : "+ commandType);
			trace("commandSenderID : " + commandSenderID);
			trace("contentData : " + contentData);
		}

		public function toByteArray():ByteArray
		{
			var myByteArray:ByteArray = new ByteArray();
			myByteArray.endian = Endian.LITTLE_ENDIAN;
			myByteArray.writeByte(this.headerFlag);
			myByteArray.writeInt(this.headerDataLength);
			myByteArray.writeUTFBytes(this.commandCode);
			myByteArray.writeByte(this.commandType);
			myByteArray.writeInt(this.commandSenderID);
			myByteArray.writeUTFBytes(this.contentData);

			return myByteArray;
		}

		public function setHeaderFlag(fl:int)
		{
			this.headerFlag = fl;
		}

		public function setHeaderDataLength(dl:int)
		{
			this.headerDataLength = dl;
		}

		public function setCommandCode(cc:String)
		{
			this.commandCode = cc;
		}

		public function setCommandType(ct:int)
		{
			this.commandType = ct;
		}

		public function setCommandSenderID(si:int)
		{
			this.commandSenderID = si;
		}

		public function setContent(ct:String)
		{
			this.contentData = ct;
		}

		public function getHeaderFlag():int
		{
			return this.headerFlag;
		}

		public function getDataLength():int
		{
			return this.headerDataLength;
		}

		public function getCommandCode():String
		{
			return this.commandCode;
		}

		public function getCommandType():int
		{
			return this.commandType;
		}

		public function getCommandSenderID():int
		{
			return this.commandSenderID;
		}

		public function getContent():String
		{
			return this.contentData;
		}

		public function packetToString():String
		{
			var str:String = "";
			str = this.headerFlag + this.headerDataLength + this.commandCode + this.commandType + this.commandSenderID + this.contentData;
			return str;
		}
	}

}